#include <iostream>
#define MAXN 100
#define MAXW 10005
using namespace std;

// 遇到循环DP进行降维操作

int N, w[MAXN], v[MAXN], W, dp[MAXN][MAXW];
void solve();

int main() {
    cin >> N;
    for (int i = 0; i < N; i++)
        cin >> w[i] >> v[i];
    cin >> W;
    solve();
    return 0;
}


// dp[i][j]从前i件挑质量为j的价值最大
// dp[i][j] = max{dp[i - 1][j - k * w[i]] + k * v[i], k >= 0}
//          = dp[i - 1][j], dp[i - 1][j -  w[i] - k * w[i]] + v[i] + k * w[i]
void solve() {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= W; j++) {
            if (j >= w[i])
                dp[i + 1][j] = max(dp[i + 1][j - w[i]] + v[i], dp[i][j]);
            else
                dp[i + 1][j] = dp[i][j];
        }
    }
    cout << dp[N][W] << endl;
}